//
//  Status.swift
//  XMGWeibo
//
//  Created by apple on 15/10/8.
//  Copyright © 2015年 小码哥. All rights reserved.
//

import UIKit

class Status: NSObject {
    /// 微博创建时间
    var created_at: String? {
        didSet {
            let date = NSDate.dateWithStr(created_at!)
            created_at = date?.dateDesc
        }
    }
    
    /// 微博ID
    var id: Int = 0
    /// 微博信息内容
    var text: String?
    /// 微博来源
    var source: String? {
        didSet {
            if let str = source {
                // "<a href="http://weibo.com" rel="nofollow">新浪微博</a>",
                if str == "" {
                    return
                }
                // 1.拿到起始位置
                let startIndex = (str as NSString).rangeOfString(">").location + 1
                // 2.拿到结束位置
                let endIndex = (str as NSString).rangeOfString("<", options: NSStringCompareOptions.BackwardsSearch).location
                // 3.截取字符串
                source = (str as NSString).substringWithRange(NSMakeRange(startIndex, endIndex - startIndex))
            }
        }
    }

    /// 配图数组
    var pic_urls: [[String: AnyObject]]? {
        didSet {
            // 1.检查配图数组是否为nil
            let count = pic_urls?.count ?? 0
            if count == 0 {
                return
            }
            // 2.生成配图的NSURL数组
            pic_URLS = [NSURL]()
            for picURLStr in pic_urls! {
                let URL = NSURL(string: picURLStr["thumbnail_pic"] as! String)
                pic_URLS?.append(URL!)
            }
        }
    }
    /// 配图URL数组
    var pic_URLS:[NSURL]?
    
    /// 用户模型
    var user: User?
    
    /// 转发微博
    var retweeted_status: Status?
    
    /// 记录cell行高
    var rowHeightOfCell: CGFloat = 0
    
    init(dict: [String: AnyObject])
    {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    // setValuesForKeysWithDictionary方法内部会调用setValue方法
    override func setValue(value: AnyObject?, forKey key: String) {
        
        // 1.判断当前的key是否是user, 如果是user就自己处理, 如果不是就让系统处理
        if key == "user"
        {
            user = User(dict: value as! [String: AnyObject])
        }else {
        
            super.setValue(value, forKey: key)
        }
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
    
    // 定义类属性数组
    static let properties = ["created_at", "id", "text", "source", "pic_urls"]
    override var description: String {
        return "\(dictionaryWithValuesForKeys(Status.properties))"
    }
}
